/**
 * 角色详情对话框
 */
var TicketInfoDlg = {
    data: {
        pid: "",
        pName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'ax',"upload"], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;
    var upload = layui.upload;
    var element = layui.element;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();
    var orderId = $("#orderId").val();
    //初始化用户下拉框
    var ajax = new $ax(Feng.ctxPath + "/staffTicket/handleData?orderId="+orderId );
    var result = ajax.start();
    var list = result;    //返回的数据
    var server = document.getElementById("handleData"); //server为select定义的id
    for(var p in list) {
        var option = document.createElement("option");  // 创建添加option属性
        option.setAttribute("value", p); // 给option的value添加值
        option.innerText = list[p];     // 打印option对应的纯文本
        server.appendChild(option);           //给select添加option子标签
        form.render("select");
    }

    //初始化用户下拉框

    var ajax = new $ax(Feng.ctxPath + "/staffTicket/userData/" );
    var result = ajax.start();
    var list = result;    //返回的数据
    var server = document.getElementById("userData"); //server为select定义的id
    for(var p in list) {
        var option = document.createElement("option");  // 创建添加option属性
        option.setAttribute("value", p); // 给option的value添加值
        option.innerText = list[p];     // 打印option对应的纯文本
        server.appendChild(option);           //给select添加option子标签
        form.render("select");
    }


    //下拉框选中事件
    form.on('select(handleData)', function (data) {
        var message=$("select[name=handleType]").val();
        if(message=="2"){
            $("#contentId").css('display','none');
            $("#content").removeAttr('lay-verify');

            $("#uploadFile").css('display','none');

            $("#userDataId").css('display','block');
            $("#userData").attr('lay-verify','required');
        }else{
            $("#userDataId").css('display','none');
            $("#userData").removeAttr('lay-verify');

            $("#contentId").css('display','block');
            $("#content").attr('lay-verify','required');
            if(message=="1"){
                $("#uploadFile").css('display','none');
                $("#contentName").text("作废原因");
                $("#contentId").css('height','250px');
                $("#contentName").attr("作废原因");
                $("#contentName").append('<span style="color: red;">*</span>');
            }else if(message=="3") {
                $("#uploadFile").css('display','block');
                $("#contentName").text("无需处理原因");
                $("#contentId").css('height','94px');

            }else if(message=="4"){
                $("#uploadFile").css('display','block');
                $("#contentName").text("无法处理原因");
                $("#contentId").css('height','94px');
                $("#contentName").append('<span style="color: red;">*</span>');
            }
        }

        $("#black").css('display','none');
        if(message==""){
            $("#contentId").css('display','none');
            $("#content").removeAttr('lay-verify');
            $("#black").css('display','block');
            $("#uploadFile").css('display','none');
        }
        admin.iframeAuto();
    });

    var imgUrl = [];
    var currentIndex=0
//拖拽上传
    var uploadInst = upload.render({
        elem: '#headImg'
        , url: Feng.ctxPath + '/staffTicket/upload'
        , size: 1000
        , multiple: true
        , number: 9

        , choose: function (obj) {
            if($(".imgDiv").length<=9){
                var files = obj.pushFile();
                //                 //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    currentIndex=index;
                    $("#demo2").append(`<div class="imgDiv" style="position: relative;width: 112px;float: left;margin-bottom: 8px;">
                    <img src="${result}" id="${index}" alt="${ file.name}" height="100px" width="100px" class="layui-upload-img uploadImgPreView" style="z-index:200;-webkit-border-radius:20px;" /> 
                    <i class="layui-icon" style="color: red;position: absolute; top: -7px;right: 5px;cursor:pointer;font-size: xx-large;"  id="remove_${index}">&#x1007;</i></div>`)
                    //$('#demo2').append('<div style="width: 112px;float: left;margin-bottom: 8px;"><img src="' + result + '" id="' + index + '" alt="' + file.name + '"height="100px" width="100px" class="layui-upload-img uploadImgPreView" style="-webkit-border-radius:20px;"> <i class="layui-icon" style="color: red;position: absolute;cursor:pointer;font-size: xx-large;"  id="remove_' + index + '">&#x1007;</i> </img></div>')
                    $('#remove_' + index).bind('click', function () {//删除指定预上传图片
                        delete files[index];//删除指定图片
                        $("#"+index).remove();
                        $(this).parent().remove();
                        var index2=imgUrl.findIndex(item=>item.index==index)
                        imgUrl.splice(index2,1)
                        var urls=[]
                        imgUrl.forEach(item=>{
                            urls.push(item.url)
                        })
                        $('#nohandleImages').val(urls.toString());
                        admin.iframeAuto();
                    })



                });
            }else{
                Feng.error("同时最多只能上传的数量为：9");
            }


        }
        , done: function (res) {
            //如果上传失败
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            //上传成功
            //打印后台传回的地址: 把地址放入一个隐藏的input中, 和表单一起提交到后台, 此处略..
                //var index=imgUrl.findIndex(item=>item.url==res.data.src)
               // var index2=imgUrl.findIndex(item=>item.index==currentIndex)
                imgUrl.push({
                    index:currentIndex,
                    url:res.data.src
                })

                var urls=[]
                imgUrl.forEach(item=>{
                    urls.push(item.url)
                })
                $('#nohandleImages').val(urls.toString());



        }
        , before: function (res) {
                imgUrl = [];

        }
        , allDone: function (res) {
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }

    });
    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/staffTicket/handleSave", function (data) {
            Feng.success("处理成功!");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("处理失败!" + data.responseJSON.message + "!");
        });
        ajax.set(data.field);
        ajax.start();

    });
});